Passed
Push — master ( 1b1d3e...93cb6f )
by
unknown
02:31
created

GetLeaveRequestsQueryHandler.canCancelLeave   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
c 0
b 0
f 0
rs 9.9
cc 3
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { GetLeaveRequestsQuery } from './GetLeaveRequestsQuery';
3
import { Inject } from '@nestjs/common';
4
import { IDateUtils } from 'src/Application/IDateUtils';
5
import { LeaveRequestView } from '../View/LeaveRequestView';
6
import { UserSummaryView } from '../../User/View/UserSummaryView';
7
import { Pagination } from 'src/Application/Common/Pagination';
8
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
9
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository';
10
import { UserNotFoundException } from 'src/Domain/HumanResource/User/Exception/UserNotFoundException';
11
import { CanLeaveRequestBeCancelled } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeCancelled';
12
13
@QueryHandler(GetLeaveRequestsQuery)
14
export class GetLeaveRequestsQueryHandler {
15
  constructor(
16
    @Inject('ILeaveRequestRepository')
17
    private readonly leaveRequestRepository: ILeaveRequestRepository,
18
    @Inject('IUserRepository')
19
    private readonly userRepository: IUserRepository,
20
    @Inject('IDateUtils')
21
    private readonly dateUtils: IDateUtils,
22
    private readonly canLeaveRequestBeCancelled: CanLeaveRequestBeCancelled
23
  ) {}
24
25
  public async execute({
26
    currentUserId,
27
    page,
28
    status
29
  }: GetLeaveRequestsQuery): Promise<Pagination<LeaveRequestView>> {
30
    const leaveRequestViews: LeaveRequestView[] = [];
31
    const [
32
      leaveRequests,
33
      total
34
    ] = await this.leaveRequestRepository.findLeaveRequests(page, status);
35
36
    const currentUser = await this.userRepository.findOneById(currentUserId);
37
    if (!currentUser) {
38
      throw new UserNotFoundException();
39
    }
40
41
    for (const leaveRequest of leaveRequests) {
42
      const leaveUser = leaveRequest.getUser();
43
44
      leaveRequestViews.push(
45
        new LeaveRequestView(
46
          leaveRequest.getId(),
47
          leaveRequest.getType(),
48
          leaveRequest.getStatus(),
49
          leaveRequest.getStartDate(),
50
          leaveRequest.getEndDate(),
51
          this.dateUtils.getLeaveDuration(
52
            leaveRequest.getStartDate(),
53
            leaveRequest.isStartsAllDay(),
54
            leaveRequest.getEndDate(),
55
            leaveRequest.isEndsAllDay()
56
          ),
57
          this.canLeaveRequestBeCancelled.isSatisfiedBy(
58
            currentUser,
59
            leaveRequest
60
          ),
61
          null,
62
          new UserSummaryView(
63
            leaveUser.getId(),
64
            leaveUser.getFirstName(),
65
            leaveUser.getLastName()
66
          )
67
        )
68
      );
69
    }
70
71
    return new Pagination<LeaveRequestView>(leaveRequestViews, total);
72
  }
73
}
74